package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.datatypewordrel.DataTypeWordRelPo;
import com.bwda.dsrs.filecenter.domain.condition.datatypewordrel.DataTypeWordRelCondition;
import com.bwda.dsrs.filecenter.domain.form.datatypewordrel.DataTypeWordRelQueryForm;
import com.bwda.dsrs.filecenter.domain.form.datatypewordrel.DataTypeWordRelUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.datatypewordrel.DataTypeWordRelCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.datatypewordrel.DataTypeWordRelVo;
import com.bwda.dsrs.filecenter.service.DataTypeWordRelService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/datatypewordrel")
@Api(value = "/b/datatypewordrel", tags = "")
public class DataTypeWordRelController extends BaseController {

	@Autowired
	private DataTypeWordRelService dataTypeWordRelService;

	@ApiOperation(value = "查询",notes = "查询",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataTypeWordRelVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer id) throws BusinessServiceException {
		DataTypeWordRelPo po = dataTypeWordRelService.queryWithValid(id);
		DataTypeWordRelVo vo = CopyUtil.transfer(po, DataTypeWordRelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数量",notes = "查询数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DataTypeWordRelQueryForm form) throws BusinessServiceException {
		DataTypeWordRelCondition condition = this.getConditionByQueryForm(form);
		int count = dataTypeWordRelService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询列表",notes = "查询列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DataTypeWordRelVo>> queryList(@ModelAttribute@Valid DataTypeWordRelQueryForm form) throws BusinessServiceException {
		DataTypeWordRelCondition condition = this.getConditionByQueryForm(form);
		List<DataTypeWordRelPo> poList = dataTypeWordRelService.queryList(condition);
		List<DataTypeWordRelVo> voList = CopyUtil.transfer(poList, DataTypeWordRelVo.class);
		return getSuccessResult(getListResponse(voList));
	}



	@ApiOperation(value = "查询列表(带分页)",notes = "查询列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DataTypeWordRelVo>> queryPageList(@ModelAttribute@Valid DataTypeWordRelQueryForm form) throws BusinessServiceException {
		DataTypeWordRelCondition condition = this.getConditionByQueryForm(form);
		List<DataTypeWordRelVo> voList = new ArrayList<>();
		int count = dataTypeWordRelService.queryCount(condition);
		if (count > 0) {
			List<DataTypeWordRelPo> poList = dataTypeWordRelService.queryList(condition);
			voList = CopyUtil.transfer(poList, DataTypeWordRelVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增",notes = "新增",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<String> add(@ModelAttribute@Valid DataTypeWordRelCreateForm form) throws BusinessServiceException {
		logger.info("绑定类别和敏感字段关系");
		DataTypeWordRelPo po = new DataTypeWordRelPo();
		DataTypeWordRelCondition condition = new DataTypeWordRelCondition();
		condition.setTypeId(form.getTypeId());
		List<DataTypeWordRelPo> dataTypeWordRelPos =dataTypeWordRelService.queryList(condition);
		//先删除
		if (dataTypeWordRelPos.size()>0){
			dataTypeWordRelPos.forEach(dataTypeWordRelPo -> {
				try {
					dataTypeWordRelService.delete(dataTypeWordRelPo.getId());
				} catch (BusinessServiceException e) {
					e.printStackTrace();
				}
			});
		}
		//后全量新增
		if (StringUtils.isNotBlank(form.getWordIds())){
			String[] wordlist = form.getWordIds().split(",");
			for (String woldIdStr:wordlist){
				po.setWordId(Integer.parseInt(woldIdStr));
				po.setTypeId(form.getTypeId());
				dataTypeWordRelService.insert(po);
			}
		}
		return getSuccessResult("操作成功");
	}


	@ApiOperation(value = "修改",notes = "修改",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DataTypeWordRelUpdateForm form) throws BusinessServiceException {
		DataTypeWordRelPo po = CopyUtil.transfer(form, DataTypeWordRelPo.class);
		dataTypeWordRelService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除",notes = "删除",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "id", required = true)@RequestParam Integer id) throws BusinessServiceException {
		dataTypeWordRelService.delete(id);
		return getSuccessResult();
	}

	/**
	 * DataTypeWordRelQueryForm转换为DataTypeWordRelCondition
	 *
	 * @param form
	 * @return
	 */
	private DataTypeWordRelCondition getConditionByQueryForm(DataTypeWordRelQueryForm form) {
		DataTypeWordRelCondition condition = CopyUtil.transfer(form, DataTypeWordRelCondition.class);
		return condition;
	}

}